package com.oa.filter;

import com.oa.entity.Employee;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter(filterName = "LoginFilter",urlPatterns = {"*Servlet","*.jsp"})
public class LoginFilter implements Filter {
	public void destroy() {
	}

	public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
		HttpServletRequest request = (HttpServletRequest) req;
		HttpServletResponse response = (HttpServletResponse) resp;

		//获取用户请求的路劲
		String path = request.getRequestURL().toString();

		//判断用户是否登入
		if(path.equals("login.jsp") || path.equals("LoginServlet")){
			chain.doFilter(req, resp);
		}else{
			//既不是登录页面，也不是登录的处理页面==》其他
			Employee loginEmp = (Employee) request.getSession().getAttribute("loginEmp");
			if (loginEmp == null){
				response.sendRedirect("login.jsp");
			}else{
				chain.doFilter(req, resp);
			}
		}
	}

	public void init(FilterConfig config) throws ServletException {

	}

}
